package com.example.viewpager;

import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;


import com.example.viewpager.adapter.Pageradapter;


public class MainActivity extends AppCompatActivity {

    private ViewPager img;
    private LinearLayout dot;

    private int[] imgs = {R.drawable.img1, R.drawable.img2, R.drawable.img3, R.drawable.img4};
    private ImageView[] dot_img = new ImageView[imgs.length];
    private Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        img = findViewById(R.id.img);
        dot = findViewById(R.id.dot);


        Pageradapter pageradapter = new Pageradapter(imgs);
        img.setAdapter(pageradapter);



        int start = pageradapter.getCount() / 2;
        int to = start - start % 4;
        img.setCurrentItem(to, false);
        initDot();
        img.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch (motionEvent.getAction()) {
                    case MotionEvent.ACTION_DOWN://点击下
                        handler.removeCallbacks(r);
                        break;
                    case MotionEvent.ACTION_UP://拿开
                    case MotionEvent.ACTION_CANCEL://拿到其他地方
                        handler.postDelayed(r, 2000);

                        break;
                }
                return false;
            }
        });

        img.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {

                                        @Override
                                        public void onPageSelected(int position) {
                                            changeDot(position % imgs.length);
                                        }
                                    }


        );
    }

    private void initDot() {
        for (int i = 0; i < imgs.length; i++) {
            //创建一个小圆点
            dot_img[i] = new ImageView(this);
            dot_img[i].setImageResource(R.drawable.select_blue);
            dot_img[i].setPadding(20, 20, 20, 20);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(50, 50);
            dot.addView(dot_img[i], params);


        }
        changeDot(0);
    }

    public void changeDot(int position) {
        for (int i = 0; i < imgs.length; i++) {
            dot_img[i].setEnabled( i==position );
        }


    }

    @Override
    protected void onStart() {
        super.onStart();
        handler.postDelayed(r, 2000);
    }

    @Override
    protected void onStop() {
        super.onStop();
        handler.removeCallbacks(r);
    }

    private Runnable r = new Runnable() {
        @Override
        public void run() {
            img.setCurrentItem(img.getCurrentItem() + 1, true);
            handler.postDelayed(r, 2000);
        }
    };
}
